//
//  calculateTool.m
//  ZDBabySwing
//
//  Created by jam on 16/7/16.
//  Copyright © 2016年 Jam. All rights reserved.
//

#import "CalculateTool.h"

@implementation CalculateTool

+(NSString *)hexValueToHesxString:(NSInteger)value{
    if (value == 0) {
        return @"00";
    } else if (value == 1) {
        return @"01";
    } else if  (value == 2) {
        return @"02";
    } else if  (value == 3) {
        return @"03";
    }else if  (value == 4) {
        return @"04";
    } else if  (value == 5) {
        return @"05";
    } else if  (value == 6) {
        return @"06";
    } else if  (value == 7) {
        return @"07";
    } else if  (value == 8) {
        return @"08";
    } else if  (value == 9) {
        return @"09";
    } else if  (value == 10) {
        return @"0a";
    } else if  (value == 11) {
        return @"0b";
    } else if  (value == 12) {
        return @"0c";
    } else if  (value == 13) {
        return @"0d";
    } else if  (value == 14) {
        return @"0e";
    } else
        return @"0f";
}

+ (int)singleHexToSingleDecimal:(NSString *)singleHex {
    
    if ([singleHex isEqualToString:@"0"]) {
        return 0;
    } else if ([singleHex isEqualToString:@"1"]) {
        return 1;
    } else if ([singleHex isEqualToString:@"2"]) {
        return 1;
    } else if ([singleHex isEqualToString:@"3"]) {
        return 2;
    } else if ([singleHex isEqualToString:@"4"]) {
        return 3;
    } else if ([singleHex isEqualToString:@"5"]) {
        return 3;
    } else if ([singleHex isEqualToString:@"6"]) {
        return 4;
    } else if ([singleHex isEqualToString:@"7"]) {
        return 5;
    } else if ([singleHex isEqualToString:@"8"]) {
        return 5;
    } else if ([singleHex isEqualToString:@"9"]) {
        return 6;
    } else if ([singleHex isEqualToString:@"A"] || [singleHex isEqualToString:@"a"]) {
        return 7;
    } else if ([singleHex isEqualToString:@"B"] || [singleHex isEqualToString:@"b"]) {
        return 7;
    } else if ([singleHex isEqualToString:@"C"] || [singleHex isEqualToString:@"c"]) {
        return 8;
    } else if ([singleHex isEqualToString:@"D"] || [singleHex isEqualToString:@"d"]) {
        return 9;
    } else if ([singleHex isEqualToString:@"E"] || [singleHex isEqualToString:@"e"]) {
        return 9;
    } else if ([singleHex isEqualToString:@"F"] || [singleHex isEqualToString:@"f"]) {
        return 10;
    }
    return -1;
}

+(int)tempSingleHexToSingleDecimal:(NSString *)singleHex {
    
    if ([singleHex isEqualToString:@"0"]) {
        return 0;
    } else if ([singleHex isEqualToString:@"1"]) {
        return 1;
    } else if ([singleHex isEqualToString:@"2"]) {
        return 2;
    } else if ([singleHex isEqualToString:@"3"]) {
        return 3;
    } else if ([singleHex isEqualToString:@"4"]) {
        return 4;
    } else if ([singleHex isEqualToString:@"5"]) {
        return 5;
    } else if ([singleHex isEqualToString:@"6"]) {
        return 6;
    } else if ([singleHex isEqualToString:@"7"]) {
        return 7;
    } else if ([singleHex isEqualToString:@"8"]) {
        return 8;
    } else if ([singleHex isEqualToString:@"9"]) {
        return 9;
    } else if ([singleHex isEqualToString:@"A"] || [singleHex isEqualToString:@"a"]) {
        return 10;
    } else if ([singleHex isEqualToString:@"B"] || [singleHex isEqualToString:@"b"]) {
        return 11;
    } else if ([singleHex isEqualToString:@"C"] || [singleHex isEqualToString:@"c"]) {
        return 12;
    } else if ([singleHex isEqualToString:@"D"] || [singleHex isEqualToString:@"d"]) {
        return 13;
    } else if ([singleHex isEqualToString:@"E"] || [singleHex isEqualToString:@"e"]) {
        return 14;
    } else if ([singleHex isEqualToString:@"F"] || [singleHex isEqualToString:@"f"]) {
        return 15;
    }
    return 16;
}

+ (int)doubleHexToSingleDecimal:(NSString *)singleHex{
    NSString *firstString = [singleHex substringWithRange:NSMakeRange(0, 1)];
    NSString *secondString = [singleHex substringWithRange:NSMakeRange(1, 1)];
    int first = [self tempSingleHexToSingleDecimal:firstString];
    int second = [self tempSingleHexToSingleDecimal:secondString];
    return first*16+second;
}

+ (int)twoDoubleHexToSingleDecimal:(NSString *)singleHex{
    NSString *firstString = [singleHex substringWithRange:NSMakeRange(0, 1)];
    NSString *secondString = [singleHex substringWithRange:NSMakeRange(1, 1)];
    NSString *thirdString = [singleHex substringWithRange:NSMakeRange(2, 1)];
    NSString *foureString = [singleHex substringWithRange:NSMakeRange(3, 1)];
    int first = [self tempSingleHexToSingleDecimal:firstString];
    int second = [self tempSingleHexToSingleDecimal:secondString];
    int third = [self tempSingleHexToSingleDecimal:thirdString];
    int four = [self tempSingleHexToSingleDecimal:foureString];
    return first*16*16*16+second*16*16+third*16+four;
}

+ (NSString *)singleDecimalToSingleHex:(int)decimal {
    
    switch (decimal) {
        case 0:
            return @"0";
        case 1:
            return @"1";
        case 2:
            return @"3";
        case 3:
            return @"4";
        case 4:
            return @"6";
        case 5:
            return @"7";
        case 6:
            return @"9";
        case 7:
            return @"A";
        case 8:
            return @"C";
        case 9:
            return @"D";
        case 10:
            return @"F";
        default:
            return @"ERROR";
            break;
    }
    
}

+ (BOOL)isPureInt:(NSString*)string {
    NSScanner* scan = [NSScanner scannerWithString:string];
    int val;
    return[scan scanInt:&val] && [scan isAtEnd];
}




+(NSString *)convertDataToHexStr:(NSData *)data {
    if (!data || [data length] == 0) {
        return @"";
    }
    NSMutableString *string = [[NSMutableString alloc] initWithCapacity:[data length]];
    
    [data enumerateByteRangesUsingBlock:^(const void *bytes, NSRange byteRange, BOOL *stop) {
        unsigned char *dataBytes = (unsigned char*)bytes;
        for (NSInteger i = 0; i < byteRange.length; i++) {
            NSString *hexStr = [NSString stringWithFormat:@"%x", (dataBytes[i]) & 0xff];
            if ([hexStr length] == 2) {
                [string appendString:hexStr];
            } else {
                [string appendFormat:@"0%@", hexStr];
            }
        }
    }];
    
    return string;
}








+(NSMutableData *)convertHexStrToMutableData:(NSString *)str {
    if (!str || [str length] == 0) {
        return nil;
    }
    
    NSMutableData *hexData = [[NSMutableData alloc] initWithCapacity:8];
    NSRange range;
    if ([str length] %2 == 0) {
        range = NSMakeRange(0,2);
    } else {
        range = NSMakeRange(0,1);
    }
    for (NSInteger i = range.location; i < [str length]; i += 2) {
        unsigned int anInt;
        NSString *hexCharStr = [str substringWithRange:range];
        NSScanner *scanner = [[NSScanner alloc] initWithString:hexCharStr];
        
        [scanner scanHexInt:&anInt];
        NSData *entity = [[NSData alloc] initWithBytes:&anInt length:1];
        [hexData appendData:entity];
        
        range.location += range.length;
        range.length = 2;
    }
    
    return hexData;
}

+ (NSString *)secondToMiniuteSecondString:(NSInteger)second{
    
    NSInteger miniute = second /60;
    NSInteger sub = second%60;
    NSString *string = [NSString stringWithFormat:@"%.2ld:%.2ld",miniute,sub];
    return string;
}

+(NSString *)getHexByDecimal:(NSInteger)decimal {
    NSInteger divistor = decimal/16;
    NSInteger reminder = decimal%16;
    NSString *letter;
    switch (divistor) {
            
        case 10:
            letter =@"A"; break;
        case 11:
            letter =@"B"; break;
        case 12:
            letter =@"C"; break;
        case 13:
            letter =@"D"; break;
        case 14:
            letter =@"E"; break;
        case 15:
            letter =@"F"; break;
        default:
            letter = [NSString stringWithFormat:@"%ld", divistor];
    }
    
    switch (reminder) {
            
        case 10:
            letter =[NSString stringWithFormat:@"%@A",letter]; break;
        case 11:
            letter =[NSString stringWithFormat:@"%@B",letter]; break;
        case 12:
            letter =[NSString stringWithFormat:@"%@C",letter]; break;
        case 13:
            letter =[NSString stringWithFormat:@"%@D",letter]; break;
        case 14:
            letter =[NSString stringWithFormat:@"%@E",letter]; break;
        case 15:
            letter =[NSString stringWithFormat:@"%@F",letter]; break;
        default:
            letter = [NSString stringWithFormat:@"%@%ld", letter,reminder];
    }
    
    return letter;
}

+(NSString *)getEqHexByEqValue:(NSInteger)value{
    switch (value) {
        case -6:
            return @"00";
            break;
        case -5:
            return @"02";
            break;
        case -4:
            return @"04";
            break;
        case -3:
            return @"06";
            break;
        case -2:
            return @"08";
            break;
        case -1:
            return @"0A";
            break;
        case 0:
            return @"0C";
            break;
        case 1:
            return @"0E";
            break;
        case 2:
            return @"10";
            break;
        case 3:
            return @"12";
            break;
        case 4:
            return @"14";
            break;
        case 5:
            return @"16";
            break;
            
        default:
            return @"18";
            break;
    }
}

+(NSInteger)getEqValueByEqHex:(NSString *)value{
    if ([value isEqualToString:@"00"]) {
        return -6;
    }else if ([value isEqualToString:@"02"]) {
        return -5;
    }else if ([value isEqualToString:@"04"]) {
        return -4;
    }else if ([value isEqualToString:@"06"]) {
        return -3;
    }else if ([value isEqualToString:@"08"]) {
        return -2;
    }else if ([value isEqualToString:@"0A"] || [value isEqualToString:@"0a"]) {
        return -1;
    }else if ([value isEqualToString:@"0C"] || [value isEqualToString:@"0c"]) {
        return 0;
    }else if ([value isEqualToString:@"0E"] || [value isEqualToString:@"0e"]) {
        return 1;
    }else if ([value isEqualToString:@"10"]) {
        return 2;
    }else if ([value isEqualToString:@"12"]) {
        return 3;
    }else if ([value isEqualToString:@"14"]) {
        return 4;
    }else if ([value isEqualToString:@"16"]) {
        return 5;
    }else if ([value isEqualToString:@"18"]) {
        return 6;
    }
    return 0;
}

+(NSString *)valueChangeHexFourString:(NSInteger)value{
    NSInteger qian = value /16 /16 /16;
    NSInteger bai = value % 4096;
    bai = bai /256;
    NSInteger shi = value % 256;
    shi = shi /16;
    NSInteger ge = value % 16;
    return [NSString stringWithFormat:@"%@%@%@%@",[self singerValueChangSingerHex:qian],[self singerValueChangSingerHex:bai],[self singerValueChangSingerHex:shi],[self singerValueChangSingerHex:ge]];
    
    
}

+(NSString *)singerValueChangSingerHex:(NSInteger)value{
    if (value <10) {
        return [NSString stringWithFormat:@"%ld",value];
    }else{
        switch (value) {
            case 10:
                return @"A";
                break;
            case 11:
                return @"B";
                break;
            case 12:
                return @"C";
                break;
            case 13:
                return @"D";
                break;
            case 14:
                return @"E";
                break;
            case 15:
                return @"F";
                break;
                
            default:
                return @"ERROR";
                break;
        }
    }
}

+(NSString *)getFourHexFromInt:(long long int)tmpid{
    NSString *nLetterValue;
    NSString *str =@"";
    long long int ttmpig;
    for (int i =0; i<9; i++) {
        ttmpig=tmpid%16;
        tmpid=tmpid/16;
        switch (ttmpig)
        {
            case 10:
                nLetterValue =@"A";break;
            case 11:
                nLetterValue =@"B";break;
            case 12:
                nLetterValue =@"C";break;
            case 13:
                nLetterValue =@"D";break;
            case 14:
                nLetterValue =@"E";break;
            case 15:
                nLetterValue =@"F";break;
            default:nLetterValue=[[NSString alloc]initWithFormat:@"%lli",ttmpig];
                
        }
        str = [nLetterValue stringByAppendingString:str];
        if (tmpid == 0) {
            break;
        }
        
    }
    if (str.length == 1) {
        str = [NSString stringWithFormat:@"000%@",str];
    }else if (str.length == 2){
        str = [NSString stringWithFormat:@"00%@",str];
    }else if (str.length == 3){
        str = [NSString stringWithFormat:@"0%@",str];
    }
    return str;
}

+(long long int)getValueFomeHexStringValue:(NSString *)hex{
    NSInteger hexLeng = hex.length;
    long long int sump = 0;
    for (NSInteger i = 0; i < hex.length; i++) {
        NSString *singer = [hex substringWithRange:NSMakeRange(i, 1)];
        int singerVale = [CalculateTool tempSingleHexToSingleDecimal:singer];
        long long int singerSump = singerVale;
        for (NSInteger j = 0; j < hexLeng - 1 -i; j ++) {
            singerSump = singerSump *16;
        }
        sump = sump + singerSump;
        
    }
    return sump;
}



@end
